package org.springframework.boot.autoconfigure.netty.security;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.springframework.boot.autoconfigure.netty.support.JwtUtils;
import org.springframework.boot.autoconfigure.netty.support.RsaUtils;

public class JwtToken implements AuthenticationToken {
 
	private static final long serialVersionUID = 1L;
 
	// 加密后的 JWT token串
	private String token;
 
	private String userName;
 
	public JwtToken(String token) {
		this.token = token;
		try {
			this.userName = JwtUtils.getClaimFiled(token, "username", RsaUtils.getPublicKey("rsa/id_rsa.pub"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(StringUtils.isEmpty(this.userName)){
			this.userName = "anonymous";
		}
	}
 
	@Override
	public Object getPrincipal() {
		return this.userName;
	}
 
	@Override
	public Object getCredentials() {
		return token;
	}
}